﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Com.DAO;
using SourceCode.Workflow.Client;
using WF.Common;
using WF.Common;
using WF.Web.Pages;

namespace WF.Web
{
    public partial class MainForm : BasePage
    {


        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!IsPostBack || this.IsRebind)
            //{
            //    this.LoadWorkList();
            //}
            ShowButton1();
        }

        #region 读取待办事项

        //private void LoadWorkList()
        //{
        //    Connection oK2Connection = WF.Common.WorkFlowHelper.GetK2Connection();
        //    WF.BusinessObject.SystemSetting sysSet = new BusinessObject.SystemSetting();
        //    string k2folder = sysSet.GetValueByParamName("K2WorkflowFolder");
        //    try
        //    {
        //        DataSet dsMe = null, dsShare = null, dsShareUser;

        //        SessionManager sm = new SessionManager();

        //        //正常的待办事项
        //        dsMe = WorkFlowHelper.GetCurrentUserWorkListItem(oK2Connection, k2folder, sm.CurrentUser.LoginName);


        //        //委托的待办事项
        //        oK2Connection.RevertUser();
        //        dsShareUser = WorkFlowHelper.GetWorkListSharedADUser(null, sm.CurrentUser.LoginName);
        //        dsShare = WorkFlowHelper.GetCurrentUserWorkListItem(oK2Connection, k2folder, sm.CurrentUser.LoginName, dsShareUser);


        //        //所有分配的待办事项
        //        DataSet dsAll = new DataSet();
        //        dsAll.Merge(dsMe);
        //        dsAll.Merge(dsShare);

        //        DataView dv = dsAll.Tables[0].DefaultView;
        //        dv.Sort = "ProcessName,StartDate Desc,RegionName,Folio";

        //        this.dataListView.DataSource = dv;
        //        this.dataListView.DataBind();

        //        //this.lblCount.Text = "(" + dv.Count.ToString() + ")";

        //    }
        //    catch (Exception ex)
        //    {
        //        CentaUtil.SendExceptionMessage(this.Page, ex);
        //    }
        //    finally
        //    {
        //        oK2Connection.Close();
        //    }
        //}

        //private void LoadWorkList1()
        //{
        //    Connection oK2Connection = WF.Common.WorkFlowHelper.GetK2Connection();
        //    try
        //    {
        //        SessionManager sm = new SessionManager();
        //        DataSet ds = WorkFlowHelper.GetCurrentUserWorkListItem(oK2Connection, sm.CurrentUser.LoginName);
        //        ds.Tables[0].DefaultView.Sort = "ProcessName,StartDate Desc";
        //        this.dataListView.DataSource = ds.Tables[0].DefaultView;
        //        this.dataListView.DataBind();
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //    finally
        //    {
        //        oK2Connection.Close();
        //    }
        //}

        #endregion


        public void ShowButton1()
        {
            dlCheckIn.Visible = true;
            string departmentLimite = new ConfigHelper().DepartmentLimite;
            string employeeLimite = new ConfigHelper().EmployeeLimite;
            //排除部门
            string[] arDepartmentLimite = departmentLimite.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            if (!string.IsNullOrEmpty(((UserProfile)Session[SessionKeys.CurrentUser]).DeptPath))
                foreach (string deptCode in arDepartmentLimite)
                {
                    if (((UserProfile)Session[SessionKeys.CurrentUser]).DeptPath.Contains(deptCode))
                    {
                        dlCheckIn.Visible = false;
                        break;
                    }
                }
            //人员例外
            if (!dlCheckIn.Visible)
            {
                string[] arEmployeeLimite = employeeLimite.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string code in arEmployeeLimite)
                {
                    if (((UserProfile)Session[SessionKeys.CurrentUser]).EmpNo == code)
                    {
                        dlCheckIn.Visible = true;
                        break;
                    }
                }
            }


        }

        protected void aCheckIn_click(object sender, EventArgs e)
        {
            string SqlTxt = "";
            string UserAccount = "";
            string CardTime = "";
            string DispInfo = "";

            Com.DAO.DataManager pobj = new DataManager();

            try
            {
                CardTime = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                UserAccount = "centaline\\" + ((UserProfile)Session[SessionKeys.CurrentUser]).LoginName.ToString();
                SqlTxt = "exec  [dbo].[HandCardTime] '" + UserAccount + "','" + CardTime + "'";
                pobj.Open(System.Configuration.ConfigurationManager.AppSettings.Get("KQConnectionString"));
                pobj.Execute(SqlTxt);
                CardTime = "您的签卡时间为:" + CardTime;

            }
            catch (Exception ex)
            {
                CardTime = "签卡错误:" + ex.Message;
                CardTime = CardTime.Replace("\r", "");
                CardTime = CardTime.Replace("\n", "");
            }
            finally
            {
                try
                {
                    if (pobj != null && !pobj.IsClosed)
                    {
                        pobj.CloseConnection();
                    }
                }
                catch (Exception)
                {
                }
                CardTime = CardTime.Replace("'", "");
                DispInfo = "";
                DispInfo = DispInfo + "<script language='javascript'> \r\n";
                DispInfo = DispInfo + "		alert(\"" + CardTime + "\");\r\n ";
                DispInfo = DispInfo + "</script>\r\n";
                //Response.Write(DispInfo);
                this.ShowMessage(CardTime, false);
            }

        }
    }
}